// pages/homepage/homepage.js
var Bmob = require("../../utils/Bmob-2.2.0.min.js")
Bmob.initialize("a25cd3ee1411fbed", "999423")

Page({
  /**
   * 页面的初始数据
   */
  data: {
    date: "",
    title: "",
    bodyId: "",
    finished: false,
    checked: false,
    actions: [{
      // name: '删除',
      // color: '#ed3f14'
      name: '确定已完成',
      color: '#09bb07'
    }],
    visible: false,
    toggle: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var login = wx.getStorageSync("usedata").status
    console.log(login)
    var that = this
    if (login) {
      //查表 条件查询根据userid（缓存中
      const query = Bmob.Query("Todo");
      query.equalTo("userId", "==", wx.getStorageSync("usedata").useId);
      query.equalTo("status", "==", false)
      query.find().then(res => {
        // console.log(res)
        that.setData({
          datas: res
        })
      });
    } else {
      //跳转登录
      wx.navigateTo({
        url: '../register/register',
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var login = wx.getStorageSync("usedata").status
    console.log(login)
    var that = this
    if (login) {
      //查表 条件查询根据userid（缓存中
      const query = Bmob.Query("Todo");
      query.equalTo("userId", "==", wx.getStorageSync("usedata").useId);
      query.equalTo("status", "==", false)
      query.find().then(res => {
        // console.log(res)
        that.setData({
          datas: res
        })
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  /**
   * 父组件时间,分析负责detail和finish事件
   */
  tap: function(e) {
    var type = e.detail.type
    if (type != undefined) {
      // console.log(e)
      if (type == "detail") {
        console.log("detail::" + e.detail.id)
        wx.setStorageSync("currentId", e.detail.id)
        wx.switchTab({
          url: '../add/add',
        })
      } else if (type == "actionsTap") {
        console.log("actionsTap::" + e.detail.id)
        var that = this
        var objectId = e.detail.id
        wx.setStorageSync("idtemp", objectId)
        this.setData({
          visible: true
        })
      }
    }
  },
  handleCancel() {
    this.setData({
      visible: false,
      toggle: this.data.toggle ? false : true
    });
    console.log(this.data.toggle, 111111111)
  },
  handleClickItem() {
    var that = this
    const action = [...that.data.actions];
    action[0].loading = true;
    that.setData({
      actions: action
    });
    var objectId = wx.getStorageSync("idtemp")
    wx.removeStorageSync("idtemp")
    var query = Bmob.Query("Todo");
    query.get(objectId).then(res => {
      console.log(res)
      res.set('status', true)
      res.save()
      console.log("gengxin")
      setTimeout(() => {
        var querynew = Bmob.Query("Todo");
        querynew.equalTo("userId", "==", wx.getStorageSync("usedata").useId);
        querynew.equalTo("status", "==", false)
        querynew.find().then(res => {
          that.setData({
            datas: res
          })
          action[0].loading = false;
          that.setData({
            visible: false,
            actions: action,
            toggle: that.data.toggle ? false : true
          });
        });
      }, 1000)
    }).catch(err => {
      console.log(err)
    })
  },
})